package com.dddd.blog.bloguser.service.impl;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import com.dddd.blog.bloguser.service.OSSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * @author 喵星球的大橘
 */
@Service
public class OSSServiceImpl implements OSSService {

    @Value("${aliyun.oss.maxSize}")
    private int maxSize;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Value("${aliyun.oss.dir.prefix}")
    private String dirPrefix;

    @Value("${aliyun.oss.urlPrefix}")
    private String urlPrefix;

    @Autowired
    private OSSClient ossClient;

    /**
     *
     * @param file
     * @return
     */
    @Override
    public String upload(MultipartFile file) throws IOException {

        if(file.isEmpty())  {
             return "";
        }
        String name = file.getOriginalFilename();

        String filePath = dirPrefix + name;

        PutObjectResult putObjectResult = ossClient.putObject(bucketName, filePath, new ByteArrayInputStream(file.getBytes()));


        return urlPrefix+"/"+filePath;
    }



}
